package netbayServlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import netbay.TransactionManager;

import util.String.StringUtil;
import util.jQgrid.JQGridFormatterUtil;

/**
 * Servlet implementation class TrafficViewerServletRD
 */
public class TrafficViewerServletRD extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public TrafficViewerServletRD() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");  // ensures that user input is interpreted as // 8-bit Unicode (e.g., for Czech characters)
		
        response.setContentType("text/json;charset=utf-8");
        HttpSession session = request.getSession();
        String url = "";
        String chkSession = "";
        chkSession = (String)session.getAttribute("session");
        String id = "";
        id = request.getParameter("id");
        request.setAttribute("converId", id);
        String userId = ((!StringUtil.isNullOrEmpty((String)(session.getAttribute("userId")))) ? (String)session.getAttribute("userId"): "null");
        if(!(userId.equals("null"))){
        	url = "/TrafficViewerRD.jsp";
        }
        else{
        	url = "/Login.jsp";
        }
        try{
        	request.getRequestDispatcher(url).forward(request, response);
		} catch (Exception ex) {
	        ex.printStackTrace();
	    }
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		 request.setCharacterEncoding("UTF-8");
		 response.setContentType("text/json;charset=utf-8");
		 PrintWriter out = response.getWriter();
	     String strJson = "{}";
	     
	     String page = request.getParameter("page"); // get the requested page
         String limit = request.getParameter("rows"); // get how many rows we want to have into the grid
         String sidx = request.getParameter("sidx"); // get index row - i.e. user click to sort
         String sord = request.getParameter("sord"); // get the direction
         
         TransactionManager tm = new TransactionManager();
         
         String converId = (!StringUtil.isNullOrEmpty(request.getParameter("converId"))) ? request.getParameter("converId").trim() : "";
		 List result = null;
		 Integer count = 0;
		 try {
			 Integer transactionId = tm.getTransactionId(converId);
			 result = tm.getDisplayTraffic(transactionId,page, limit, sidx, sord);
			 count = tm.getCountTraffic(transactionId);
			 
			 strJson = JQGridFormatterUtil.getJSON(Integer.parseInt(page), Integer.parseInt(limit), count, result);
			 //out.println(strJson);
         } catch (Exception ex) {
        	 strJson = "{\"error\": \"" + ex.getMessage() + "\"}" ;
         }
         out.println(strJson);
	}

}
